package ferp.center.server.servlet.config;

import ferp.center.network.response.ResponseConfigGet;
import ferp.center.server.Action;
import ferp.center.server.entity.Config;

import javax.persistence.EntityManager;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * User: igorgok
 * Date: 12/9/12 5:12 PM
 */
public class ConfigUpdate extends HttpServlet
{
  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
  {
    Action.execute(request, new Action.Task()
    {
      @Override
      protected void run(EntityManager em, HttpServletRequest request) throws IOException
      {
        Config old = ConfigGet.current(em);

        if (old != null)
        {
          log("updating configuration...");

          ResponseConfigGet.Ad ad = new ResponseConfigGet.Ad();

          ad.interstitial = new ResponseConfigGet.Ad.Interstitial();
          ad.interstitial.enabled = true;

          old = em.merge(old);
          old.setAd(ad);

          ConfigGet.uncache();

          log("done.");
        }
      }
    });
  }
}
